package managerGUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import managerService.ManagerSystem;
import model.Project;



public class MainInterface extends JFrame{

	private static final long serialVersionUID = 1L;
	
	public static MainInterface instance = null;
	private ManagerMenu menu;
	private JScrollPane scrollPane;
	private JList<Project> list;
	
	/*=========================== getters ==========================*/
	
	public ManagerMenu getMenu(){
		return menu;
	}
	
	public ManagerPanel getPanel(){
		return getPanel();
	}


	public static MainInterface getInstance(){
		return instance;
	}
	
	public MainInterface(){
		menu = new ManagerMenu();
		
		this.setJMenuBar(menu);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{712, 0};
		gridBagLayout.rowHeights = new int[]{401, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 0;
		getContentPane().add(scrollPane, gbc_scrollPane);
		
		ProjectListModel listModel = new ProjectListModel();
		ManagerSystem.getService().setListModel(listModel);
		
		
		list = new JList<Project>(listModel);
		list.setCellRenderer(new ProjectListRenderer());

		scrollPane.setViewportView(list);
		setSize((int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth()*0.5),
				(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight()*0.5));
		setLocationRelativeTo(null);
		setTitle("Project Manager");
		setVisible(true);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e){
				int option = JOptionPane.showConfirmDialog(null,
						"Exit program?",
						"Exit",
						JOptionPane.OK_CANCEL_OPTION,
						JOptionPane.QUESTION_MESSAGE);
				if (option == JOptionPane.OK_OPTION)
					System.exit(0);        
			}
			});
		
		instance = this;
	}
	
}